package org.ndx.majick.properties.util;

import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.ndx.majick.ui.IClassUIProvider;

/**
 * ClassMap is an extension of maps suited for maps only.
 * Additionnally to classical map features, it provides the {@link #locate(Class)} method, which allows finding of the nearest matching value for class hierarchy
 * @author Nicolas Delsaux
 *
 * @param <T> class of holded values
 */
public class ClassMap<T extends Object> extends HashMap<Class<?>, T> {
	/**
	 * Class internal logger. MUST NOT be used anywhere else, for logs readability
	 */
	private static final Logger logger = Logger.getLogger(ClassMap.class
			.getName());

	public T locate(Class<?> key) {
		if(containsKey(key)) {
			T returned = get(key);
			if (logger.isLoggable(Level.FINE))
				logger.log(Level.FINE, "class "+key.getName()+" is associated to "+returned.getClass().getName());
			return returned;
		} else {
			return locate(key.getSuperclass());
		}
	}

}
